package com.hup.utils.swing;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import javax.swing.*;

/**
 * @author hugan
 * @date 2019/10/9
 */
public class HtmlUtil {

    public static String CENTER = "<style>"
            + "td {text-align:center}"
            + "</style>";

    public static <T> String getOneColumnHtml(String head, Iterable<T> list, HtmlGetter<T> getter) {
        return getOneColumnHtml(head, list, getter, null);
    }

    public static <T> String getOneColumnHtml(String head, Iterable<T> list, HtmlGetter<T> getter, String style) {
        StringBuilder sb = new StringBuilder();
        sb.append("<html>");
        if (style != null) {
            sb.append(style);
        }
        sb.append("<table cellpadding='1'");//cellpadding:单元格紧凑
        sb.append(" style='");
        //sb.append(" font-family: 宋体;");
        sb.append(" font-weight: normal;'>");
        sb.append("<tr><td><b>");
        sb.append(head);
        sb.append("</b></td></tr>");
        for (T o : list) {
            sb.append("<tr><td>");
            getter.appendHtmlText(o, sb);
            sb.append("</td></tr>");
        }
        sb.append("</table></html>");
        return sb.toString();
    }

    public interface HtmlGetter<T> {
        void appendHtmlText(T o, StringBuilder sb);
    }

    public interface RowTextAppender<T> {
        void append(StringBuilder sb, T item);
    }

    @NoArgsConstructor(access = AccessLevel.PRIVATE)
    public static class LineWrapHtmlAppender {
        private final StringBuilder sb = new StringBuilder("<html>");
        private boolean hadAppend = false;
        private Integer align;

        public static LineWrapHtmlAppender instance() {
            return new LineWrapHtmlAppender();
        }

        /**
         * @param align {@link SwingConstants#CENTER}, {@link SwingConstants#RIGHT}
         */
        public static LineWrapHtmlAppender instance(int align) {
            LineWrapHtmlAppender res = new LineWrapHtmlAppender();
            res.align = align;
            if (align == SwingConstants.CENTER) {
                res.sb.append("<p align='center'>");
            } else if (align == SwingConstants.RIGHT) {
                res.sb.append("<p align='right'>");
            }
            return res;
        }

        public LineWrapHtmlAppender append(String line) {
            hadAppend = true;
            sb.append(line).append("<br>");
            return this;
        }

        public LineWrapHtmlAppender append(Object[] objs) {
            hadAppend = true;
            for (Object o : objs) {
                sb.append(o).append("<br>");
            }
            return this;
        }

        public String getHtml() {
            if (hadAppend) {
                sb.delete(sb.length() - 4, sb.length());
            }
            if (align != null && (align == SwingConstants.CENTER || align == SwingConstants.RIGHT)) {
                sb.append("</p>");
            }
            return sb.append("</html>").toString();
        }
    }

}
